package app

import (
	"godemo/app/controllers"
	"godemo/app/middlewares"
	"godemo/app/models"
	"godemo/common"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

const SHORT = 2 * time.Second

func init() {
	common.InitMysql()
}
func Start() {

	r := gin.Default()
	r.Use(middlewares.StartSession())
	//解析静态文件目录
	r.StaticFS("/static", http.Dir("app/static"))
	//设置视图文件
	r.LoadHTMLGlob("app/views/*")

	//未登录，跳转到登陆页面
	r.GET("/", func(c *gin.Context) {
		//c.Request.URL.Path = "/login"
		//r.HandleContext(c)
		c.Redirect(http.StatusMovedPermanently, "/user/index")

	})

	r.GET("/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "login.html", gin.H{"title": "用户登陆"})
	})

	r.POST("/login", controllers.UserLogin)
	r.GET("/logout", controllers.UserLogOut)

	r.GET("/captcha/get", models.GenerateCaptchaHandler)
	r.POST("/captcha/verify", models.CaptchaVerifyHandle)

	r.GET("/user/index", middlewares.ValidLogin(), controllers.UserIndex)
	r.GET("/user/del", middlewares.ValidLogin(), controllers.UserDel)

	r.NoRoute(func(c *gin.Context) {
		c.HTML(http.StatusNotFound, "404.html", gin.H{"title": "页面未找到"})
	})

	r.Run(":9999")

}
